package com.zhengjiang.nio;

import java.nio.CharBuffer;

/**
 * @author zhengjiang
 * @version 1.0.0
 * @description
 * @date 2021/6/5
 **/
public class BufferTest {

    public static void main(String[] args) {
        CharBuffer buffer = CharBuffer.allocate(10);
        System.out.println("=================init buffer===========");
        System.out.println("buffer.limit:" + buffer.limit());
        System.out.println("buffer.capacity:" + buffer.capacity());
        System.out.println("buffer.position:" + buffer.position());
        buffer.put("a");
        buffer.put("b");
        buffer.put("c");
        System.out.println("=================after put ===========");
        System.out.println("buffer.limit:" + buffer.limit());
        System.out.println("buffer.capacity:" + buffer.capacity());
        System.out.println("buffer.position:" + buffer.position());
        buffer.flip();// 调用flip()后 降limit设置为当前position位置,把position设为0,limit之后的空间不可访问
        System.out.println("=================after flip ===========");
        System.out.println("buffer.limit:" + buffer.limit());
        System.out.println("buffer.capacity:" + buffer.capacity());
        System.out.println("buffer.position:" + buffer.position());
        System.out.println(buffer.get());
        System.out.println(buffer.get());
        System.out.println(buffer.get());
//        System.out.println(buffer.get(3)); // 此处会报错
        buffer.clear();// 不清空buffer中的数据,只是将position置为0,将limit置为capacity,为再次向buffer中装入数据准备
        System.out.println("=================after clear ===========");
        System.out.println("buffer.position:" + buffer.position());
        System.out.println(buffer.get());
        System.out.println(buffer.get());
        System.out.println(buffer.get());
        System.out.println("buffer.position:" + buffer.position());

    }
}
